package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.AddMenberform;
import com.woniuxy.controller.form.DeleMenberforom;
import com.woniuxy.controller.form.FindAllMenberbyLikeform;
import com.woniuxy.controller.form.FindAllMenberform;
import com.woniuxy.controller.info.Findmenmberinfo;
import com.woniuxy.dao.ChannelDao;
import com.woniuxy.dao.MemberChannelRelDao;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.DelechannelIsNullExption;
import com.woniuxy.exception.ShopCodeCopeExption;
import com.woniuxy.model.Channel;
import com.woniuxy.model.Member;
import com.woniuxy.dao.MemberDao;
import com.woniuxy.model.MemberChannelRel;
import com.woniuxy.service.MemberChannelRelService;
import com.woniuxy.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-28
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberDao, Member> implements MemberService {

    @Resource
    private MemberChannelRelDao memberChannelDao;
    @Resource
    private ChannelDao channelDao;
    @Resource
    private MemberDao memberDao;
    @Resource
    private MemberChannelRelDao memberChannelRelDao;

    /**
     * 分页查询所有会员
     * @param findAllMenberform
     * @return
     */
    @Override
    public Result findMemberpage(FindAllMenberform findAllMenberform) {
        Page<Member> Menberpage= baseMapper.selectPage(findAllMenberform, null);
        List<Member> records = Menberpage.getRecords();
        List<Findmenmberinfo> findmenmberinfos = NewCopyBean.copyList(records, Findmenmberinfo.class);
        Page<Findmenmberinfo> findmenmberinfoPage = new Page<>();
        for (Findmenmberinfo findmenmberinfo : findmenmberinfos) {
            MemberChannelRel member_id = memberChannelDao.selectOne(new QueryWrapper<MemberChannelRel>().eq("member_id", findmenmberinfo.getMemberId()));
            Channel channel_id = channelDao.selectOne(new QueryWrapper<Channel>().eq("channel_id", member_id.getChannelId()));
            findmenmberinfo.setChannelName(channel_id.getChannelName());
        }
        findmenmberinfoPage.setRecords(findmenmberinfos);
        findmenmberinfoPage.setSize(Menberpage.getSize());
        findmenmberinfoPage.setCurrent(Menberpage.getCurrent());
        findmenmberinfoPage.setTotal(Menberpage.getTotal());
        return Result.success(200,"查询成功",findmenmberinfoPage);
    }

    /**
     * 分页模糊查询所有会员
     * @param findAllMenberbyLikeform
     * @return
     */
    @Override
    public Result findMemberpagebylike(FindAllMenberbyLikeform findAllMenberbyLikeform) {
        Page<Member> Memberpage = baseMapper.selectPage(findAllMenberbyLikeform, new QueryWrapper<Member>()
                .like("member_name", findAllMenberbyLikeform.getLikeName())
                .or().like("member_phone", findAllMenberbyLikeform.getLikeName()));
        List<Member> records = Memberpage.getRecords();
        List<Findmenmberinfo> findmenmberinfos = NewCopyBean.copyList(records, Findmenmberinfo.class);
        Page<Findmenmberinfo> findmenmberinfoPage = new Page<>();
        for (Findmenmberinfo findmenmberinfo : findmenmberinfos) {
            MemberChannelRel member_id = memberChannelDao.selectOne(new QueryWrapper<MemberChannelRel>().eq("member_id", findmenmberinfo.getMemberId()));
            Channel channel_id = channelDao.selectOne(new QueryWrapper<Channel>().eq("channel_id", member_id.getChannelId()));
            findmenmberinfo.setChannelName(channel_id.getChannelName());
        }
        findmenmberinfoPage.setRecords(findmenmberinfos);
        findmenmberinfoPage.setSize(Memberpage.getSize());
        findmenmberinfoPage.setCurrent(Memberpage.getCurrent());
        findmenmberinfoPage.setTotal(Memberpage.getTotal());
           return Result.success(200,"查询成功",findmenmberinfoPage);
    }

    /**
     * 传回一个或者多个对象集合，
     * 根据对象id进去循环删除会员对象
     * 并且删除会员渠道商关联表中对应的关联信息
     * 同时相对应的渠道商会员数-1
     * @param deleMenberforom
     * @return
     * @throws DdException
     */
    @Override
    public Result deleMenbers(DeleMenberforom deleMenberforom) throws DdException {
        List<Member> members = deleMenberforom.getMembers();
        if (ObjectUtils.isEmpty(members)) throw new DelechannelIsNullExption(5004,"请先勾选要删除的渠道商");
        for (Member listmember : members) {

            String channel_id = memberChannelDao.selectOne(new QueryWrapper<MemberChannelRel>().eq("member_id", listmember.getMemberId()))
                    .getChannelId();
            Channel channel_id1 = channelDao.selectOne(new QueryWrapper<Channel>().eq("channel_id", channel_id));
            channel_id1.setChannelMemberNum(channel_id1.getChannelMemberNum()-1);
            channelDao.update(channel_id1,new QueryWrapper<Channel>().eq("channel_id",channel_id));
            baseMapper.delete(new QueryWrapper<Member>().eq("member_id",listmember.getMemberId()));
            memberChannelDao.delete(new QueryWrapper<MemberChannelRel>().eq("member_id",listmember.getMemberId()));
        }
        return Result.success(200,"删除成功",null);

    }

    /**
     * 先判断重复 做去重异常
     * 然后再根据传回来的参数进行新增
     * 同时添加渠道商会员关联表数据
     * 同时添加渠道商旗下会员数量
     * @param addMenberform
     * @return
     * @throws DdException
     */
    @Override
    public Result addMember(AddMenberform addMenberform) throws DdException {
        System.out.println(addMenberform);
        if (!ObjectUtils.isEmpty(memberDao.selectOne(new QueryWrapper<Member>().eq("member_id",addMenberform.getMemberId()))))
            throw new ShopCodeCopeExption(50002,"该会员编号已存在重新输入");
//
        memberChannelDao.insert(new MemberChannelRel(addMenberform.getChannelId(),addMenberform.getMemberId()));//添加关联表
        memberDao.insert(new Member(addMenberform.getMemberId(),addMenberform.getMemberName(),
                addMenberform.getMemberPhone(),addMenberform.getMemberYue(),
                addMenberform.getMemberFreeze(),addMenberform.getMemberScore(),
                addMenberform.getMemberStat(),addMenberform.getMemberBank(),
                addMenberform.getMemberBankAccount(),addMenberform.getMemberIdNumber(),
                addMenberform.getMemberBirthday(),addMenberform.getMemberLeve1()
        ,new Date(System.currentTimeMillis())));
        Channel channel = channelDao.selectOne(new QueryWrapper<Channel>().eq("channel_id", addMenberform.getChannelId()));
        channel.setChannelMemberNum(channel.getChannelMemberNum()+1);
        channelDao.update(channel,new QueryWrapper<Channel>().eq("channel_id",addMenberform.getChannelId()));
        //找出
        return Result.success(200,"新增成功",null);
    }
}
